﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace PServiceBus.Core.Runtime.Extensions {
    /// <summary>
    /// Retrieved from http://stackoverflow.com/a/4036187/607310
    /// </summary>
    public static class TypeExtension {
        private class SimpleTypeComparer : IEqualityComparer<Type> {
            public bool Equals(Type x, Type y) {
                return x.Assembly == y.Assembly &&
                    x.Namespace == y.Namespace &&
                    x.Name == y.Name;
            }

            public int GetHashCode(Type obj) {
                return obj.GetHashCode();
            }
        }

        public static MethodInfo GetGenericMethod(this Type type, string name, Type[] parameterTypes) {
            var methods = type.GetMethods();
            foreach (var method in methods.Where(m => m.Name == name)) {
                var methodParameterTypes = method.GetParameters().Select(p => p.ParameterType).ToArray();

                if (methodParameterTypes.SequenceEqual(parameterTypes, new SimpleTypeComparer())) {
                    return method;
                }
            }

            return null;
        }
    }
}
